package com.youan.serve;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("group1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.youan.serve.controller"))   //指定扫描的包
                .paths(PathSelectors.any())    // 过滤什么路径
                .build();
        //  replaceDefaultPlugin();
        return docket;
    }

    /** 替换默认的PluginRegistry插件 */
/*    private void replaceDefaultPlugin() {
        // 插件集合
        BeanDefinitionRegistry beanDefReg = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        beanDefReg.removeBeanDefinition("operationParameterReader");

        BeanDefinitionBuilder beanDefBuilder = BeanDefinitionBuilder.genericBeanDefinition(PanguOperationBuilderPlugin.class);
        BeanDefinition beanDef = beanDefBuilder.getBeanDefinition();
        beanDefReg.registerBeanDefinition("operationParameterReader", beanDef);
    }*/

    //配置swagger信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("山东右安软件开发组", "url", "email");
        return new ApiInfo("pangu",
                "基础项目",
                "1.0",
                "http://localhost",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}